<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

class JFX_Page
{
    protected $details = array(
        'id' => '',
        'parent_id' => '',
        'slug' => '',
        'is_published' => '',
        'workspace_id' => '',
        'owner_id' => '',
        'created_tstamp' => '',
        'modified_tstamp' => '',
        'is_default' => '',
        'is_template' => '',
        'domain_id' => '',
        'sorting' => ''
    );

    /**
     * Use this method to create a new page, for instance inserting a new page to db
     */
    public function __construct(){
        // get some default values that will make it work when saving a new page
        $USER = JFX::registry('JFX_User');
        $SETTINGS = JFX::registry('JFX_Settings');
        $DB = JFX::registry('db');
        $CONFIG = JFX::registry('config');

        $this->set('workspace_id', $USER->workspace);
        $this->set('created_tstamp', time());
        $this->set('modified_tstamp', 0);
        $this->set('is_default', 0);
        $this->set('is_template', 0);
        $this->set('domain_id', $DB->oneValue($CONFIG->dbprefix.'domains', 'id', 'is_default = 1'));

    }

    public $title = '';

    public static function getUniqueSlug($preferredSlug, $workspaceid, $domainid){
        $DB = JFX::registry('db');
        $CONFIG = JFX::registry('config');
        
        if($DB->countRows($CONFIG->dbprefix.'pages', "workspace_id = '{$workspaceid}' AND domain_id = '{$domainid}' AND slug = '{$slug}'")>0){
            $newPreferredSlug .= '1';
            return self::getUniqueSlug($newPreferredSlug);
        }else{
            // not already existing
            return $preferredSlug;
        }

    }

    public function loadById($id){
        $DB = JFX::registry('db');
        $CONFIG = JFX::registry('config');
        $CORE = JFX::registry('JFX_Core');
        $id = $DB->escape($id);
        if($DB->countRows($CONFIG->dbprefix.'pages', "id = '{$id}'")==0) return false;
        $this->set($DB->fetchRow("SELECT * FROM {$CONFIG->dbprefix}pages WHERE id = '{$id}'"));
        $this->title = $CORE->getContentLang('page_title', $id);
        return true;
    }

    public function get($key=null){
        $retArray = $this->details;
        $retArray['title'] = $this->title;
        if($key!==null && array_key_exists($key, $retArray)) return $retArray[$key];
        else return $retArray;
    }

    public function set($key, $value=null){
        if(is_array($key)){
            foreach($key as $k=>$v){
                $this->set($k, $v);
            }
        }else if(!is_string($key)){
            return false;
        }else{
            if(array_key_exists($key, $this->details)) $this->details[$key] = $value;
            if($key==='title'){
                $this->title = $title;
            }
        }
        return true;
    }

    public function canSave(){
        if($this->get('workspace_id')!='' && $this->get('slug')!='' && $this->get('domain_id')!=''){
            return true;
        }
        return false;
    }

    public function save(){
        $DB = JFX::registry('db');
        $CONFIG = JFX::registry('config');
        $CORE = JFX::registry('JFX_Core');
        if(!$this->canSave()) return false;
        $error = false;
        if($this->get('id')==''){
            // inserting
            $pageid = $DB->insert($CONFIG->dbprefix.'pages', $this->get());
            if(intval($pageid)===0){
                $error = true;
            }else{
                $error = false;
            }
            $this->set('id', $pageid);
            
        }else{
            // updating
            $dbParams = $this->get();
            $pageid = $dbParams['id'];
            unset($dbParams['id']);
            $DB->update($CONFIG->dbprefix.'pages', $dbParams, "id = '{$pageid}'");
        }

        if(!$error){
            $CORE->setContentLang('page_title', $pageid, $this->title);
        }

        return !$error;
        
    }
}
